package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysEmployee;
import com.ruoyi.system.mapper.SysEmployeeMapper;
import com.ruoyi.system.service.ISysEmployeeService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 人员管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-26
 */
@Service
public class SysEmployeeServiceImpl implements ISysEmployeeService
{
    @Autowired
    private SysEmployeeMapper sysEmployeeMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询人员管理
     * 
     * @param id 人员管理ID
     * @return 人员管理
     */
    @Override
    public SysEmployee selectSysEmployeeById(Long id)
    {
        return sysEmployeeMapper.selectSysEmployeeById(id);
    }

    /**
     * 查询人员管理列表
     * 
     * @param sysEmployee 人员管理
     * @return 人员管理
     */
    @Override
    public List<SysEmployee> selectSysEmployeeList(SysEmployee sysEmployee)
    {
        // List<Long> deptIdList = getDeptIds(sysEmployee.getDeptId());
        return sysEmployeeMapper.selectSysEmployeeList(sysEmployee);
    }

    public List<Long> getDeptIds(Long deptId) {
        List<Long> idList = sysEmployeeMapper.getAllDeptIds2(deptId);

        /*List<Long> idList = null;
        if(null != deptId){
            List<Map<Long, Long>> deptIds = sysEmployeeMapper.getAllDeptIds();
            // 根据order排序
            deptIds.sort(Comparator.comparing(x -> x.get("dept_id")));
            idList = getChildrens(deptId, deptIds);
            idList.add(deptId);
        }*/
        return idList;
    }

    private List<Long> getChildrens(Long deptId, List<Map<Long, Long>> deptIds) {
        //最终得到的子孙集合
        List<Long> idList = new ArrayList<Long>();
        if(deptId!=null) {
            for (int i = 0; i < deptIds.size(); i++) {
                if(deptId == deptIds.get(i).get("parent_id")){
                    idList.add(deptIds.get(i).get("dept_id"));
                    getChildrens(deptIds.get(i).get("dept_id"), deptIds);
                };
            }
        }
        return idList;
    }

    /**
     * 新增人员管理
     * 
     * @param sysEmployee 人员管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {CustomException.class, Exception.class})
    public int insertSysEmployee(SysEmployee sysEmployee)
    {
        sysEmployee.setCreateTime(DateUtils.getNowDate());
        SysUser sysUser = new SysUser();
        sysUser.setEmail(sysEmployee.getEmail());
        sysUser.setPhonenumber(sysEmployee.getPhone());
        sysUser.setRemark(sysEmployee.getRemark());
        sysUser.setUserName(sysEmployee.getPhone());
        sysUser.setRealName(sysEmployee.getName());
        sysUser.setDeptId(sysEmployee.getDeptId());
        userService.insertUser(sysUser);
        return sysEmployeeMapper.insertSysEmployee(sysEmployee);
    }

    /**
     * 修改人员管理
     * 
     * @param sysEmployee 人员管理
     * @return 结果
     */
    @Override
    public int updateSysEmployee(SysEmployee sysEmployee)
    {
        sysEmployee.setUpdateTime(DateUtils.getNowDate());
        return sysEmployeeMapper.updateSysEmployee(sysEmployee);
    }

    /**
     * 批量删除人员管理
     * 
     * @param ids 需要删除的人员管理ID
     * @return 结果
     */
    @Override
    public int deleteSysEmployeeByIds(Long[] ids)
    {
        return sysEmployeeMapper.deleteSysEmployeeByIds(ids);
    }

    /**
     * 删除人员管理信息
     * 
     * @param id 人员管理ID
     * @return 结果
     */
    @Override
    public int deleteSysEmployeeById(Long id)
    {
        return sysEmployeeMapper.deleteSysEmployeeById(id);
    }

    @Override
    public List<String> getOutletsByDeptIds(List<Long> deptIdList) {
        return sysEmployeeMapper.getOutletsByDeptIds(deptIdList);
    }

    @Override
    public List<Long> getProjectByRoleIds(List<Long> roleIds) {
        return sysEmployeeMapper.getProjectByRoleIds(roleIds);
    }

    @Override
    public List<Long> getRoleIdByUserId(Long userId) {
        return sysEmployeeMapper.getRoleIdByUserId(userId);
    }

    @Override
    public Integer checkPhoneUnique(String phone) {
        return sysEmployeeMapper.checkPhoneUnique(phone);
    }
}